package com.zy.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zy.dao.MemberDao;
import com.zy.pojo.Member;
import com.zy.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
	@Autowired
	private MemberDao memberDao;
	@Override
	public Member findByTelephone(String telephone) {
		//先判断当前手机号是否为会员
		Member member = memberDao.findByTelephone(telephone);
		if(member == null){
			//自动注册为会员
			member = new Member();
			member.setPhoneNumber(telephone);
			member.setRegTime(new Date());
			memberDao.add(member);
		}
		return member;
	}

	/**
	 * 根据月份集合，查询出会员数集合
	 * @param months  月份集合
	 * @return
	 */
	@Override
	public List<Integer> findMemberCountByMonth(List<String> months) throws  Exception {

		List<Integer> list = new ArrayList<>();

		for (String month : months) {
			//转为当月末
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date dBegin = sdf.parse(month);
			dBegin.setMonth(dBegin.getMonth()+1); //月加一
			dBegin.setDate(dBegin.getDate()-1); //日减一
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
			String date = sdf1.format(dBegin);
			System.out.println("曲线图测试"+date);
			Integer count=memberDao.findMemberCountByMonth(date);
			list.add(count);
		}


		return list;
	}
}
